﻿using System.Collections.Generic;
using System.Configuration;

//http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx
//http://www.codeproject.com/Articles/16466/Unraveling-the-Mysteries-of-NET-2-0-Configuration
namespace Mvc3Demo.Core
{
    public class ViewTypeSettingGroup : ConfigurationSectionGroup
    {

    }

    public class ViewTypeSettings : ConfigurationSection
    {
        static ViewTypeSettings()
        {
            Settings = ConfigurationManager.GetSection("viewTypeSettings") as ViewTypeSettings;
        }

        public static ViewTypeSettings Settings { get; private set; }

        [ConfigurationProperty("viewTypes")]
        public ViewTypeCollection ViewTypes { get { return this["viewTypes"] as ViewTypeCollection; } }

        [ConfigurationProperty("viewsInFolder", DefaultValue = true)]
        public bool ViewsInFolder
        {
            get { return (bool)this["viewsInFolder"]; }
            set { this["viewsInFolder"] = value; }
        }
    }

    [ConfigurationCollection(typeof(ViewType), AddItemName = "viewType",
        CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class ViewTypeCollection : ConfigurationElementCollection, IEnumerable<ViewType>
    {
        #region Constructors

        static ViewTypeCollection()
        {
            _properties = new ConfigurationPropertyCollection();
        }

        #endregion

        #region Fields

        private static readonly ConfigurationPropertyCollection _properties;

        #endregion

        #region Properties

        protected override ConfigurationPropertyCollection Properties
        {
            get { return _properties; }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
        }

        #endregion

        #region indexer

        public ViewType this[int index]
        {
            get { return (ViewType)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }

        public new ViewType this[string name]
        {
            get { return (ViewType)BaseGet(name); }
        }

        #endregion

        #region overriden methods

        protected override ConfigurationElement CreateNewElement()
        {
            return new ViewType();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as ViewType).Name;
        }

        public new IEnumerator<ViewType> GetEnumerator()
        {
            var baselsit = base.GetEnumerator();

            while (baselsit.MoveNext())
            {
                yield return baselsit.Current as ViewType;
            }
        }

        #endregion
    }

    public class ViewType : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        //[StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
        [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 0, MaxLength = 256)]
        public string Name
        {
            get { return this["name"] as string; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("deviceIdentifiers", IsRequired = true)]
        [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 0, MaxLength = 256)]
        public string DeviceIdentifiers
        {
            get { return this["deviceIdentifiers"] as string; }
            set { this["deviceIdentifiers"] = value; }
        }

    }
}